//
//  NTESLiveInnerView.m
//  Netease
//
//  Created by 王方伟 on 2017/8/28.
//  Copyright © 2017年 wfw. All rights reserved.
//

#import "NTESLiveInnerView.h"
#import "NTESLiveroomInfoView.h"
#import "NTESLivePresentView.h"
#import "NTESCameraZoomView.h"
#import "NTESLiveBypassView.h"
#import "NTESLiveActionView.h"
#import "NTESAnchorMicView.h"
#import "NTESLiveCoverView.h"
#import "NTESTextInputView.h"
#import "NTESLiveChatView.h"
#import "NTESLiveLikeView.h"
#import "NTESLiveManager.h"
#import "NTESTimerHolder.h"
#import "NTESGLView.h"

@interface NTESLiveInnerView ()<NTESLiveActionViewDelegate, NTESTextInputViewDelegate, NTESTimerHolderDelegate, NTESLiveBypassViewDelegate, NTESLiveCoverViewDelegate,NTESLiveChatViewDelegate>
//聊天室ID
@property (copy, nonatomic) NSString *roomId;
//关闭直播按钮
@property (nonatomic, strong) UIButton *closeButton;

//状态覆盖层
@property (strong, nonatomic) NTESLiveCoverView *coverView;
//直播间信息视图
@property (strong, nonatomic) NTESLiveroomInfoView *infoView;
//输入框
@property (strong, nonatomic) NTESTextInputView *textInputView;
//聊天窗
@property (strong, nonatomic) NTESLiveChatView *chatView;
//操作条
@property (strong, nonatomic) NTESLiveActionView *actionView;
//爱心视图
@property (strong, nonatomic) NTESLiveLikeView *likeView;
//礼物到达视图
@property (strong, nonatomic) NTESLivePresentView *presentView;
//互动直播的小视图
@property (strong, nonatomic) NTESLiveBypassView *bypassView;
//接收YUV数据的视图
@property (strong, nonatomic) NTESGLView *glView;
//主播是音视频的时候的麦克风图
@property (strong, nonatomic) NTESAnchorMicView *micView;
//互动直播昵称
@property (nonatomic, strong) UILabel *bypassNickLabel;
//房间ID
@property (nonatomic, strong) UILabel *roomIdLabel;
@property (strong, nonatomic) NTESCameraZoomView *cameraZoomView;
//actionView上移标识
@property (assign, nonatomic) BOOL isActionViewMoveUp;

@property (assign, nonatomic) CGFloat keyBoradHeight;
@property (strong, nonatomic) NTESTimerHolder *timer;
@property (assign, nonatomic) CGSize lastRemoteViewSize;

@property (assign, nonatomic) BOOL isBig;
@end

@implementation NTESLiveInnerView

- (UIButton *)closeButton{
    if(!_closeButton){
        
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage imageNamed:@"icon_close_n"] forState:UIControlStateNormal];
        [_closeButton setImage:[UIImage imageNamed:@"icon_close_p"] forState:UIControlStateHighlighted];
        [_closeButton addTarget:self action:@selector(onClose:) forControlEvents:UIControlEventTouchUpInside];
        _closeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
        _closeButton.size = CGSizeMake(44, 44);
        _closeButton.top = 5.f;
        _closeButton.right = self.width - 5.f;
    }
    return _closeButton;
}

- (NTESLiveroomInfoView *)infoView{
    if (!_infoView) {
        _infoView = [[NTESLiveroomInfoView alloc] initWithFrame:CGRectMake(10, 10, 0, 0)];
        [_infoView sizeToFit];
        _infoView.hidden = YES;
    }
    return _infoView;
}


- (NTESTextInputView *)textInputView{
    if (!_textInputView) {
        CGFloat height = 44.f;
        _textInputView = [[NTESTextInputView alloc] initWithFrame:CGRectMake(0, 0, self.width, height)];
        _textInputView.delegate = self;
        _textInputView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
        _textInputView.hidden = YES;
        NTESGrowingTextView *textView = _textInputView.textView;
        NSString *placeHolder = @"聊天室连接中，暂时无法发言";
        textView.editable = NO;
        textView.placeholderAttributedText = [[NSAttributedString alloc] initWithString:placeHolder attributes:@{NSFontAttributeName:textView.font,NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
    }
    return _textInputView;
}

- (NTESLiveChatView *)chatView{
    if (!_chatView) {
        CGFloat height = 85.f;
        CGFloat width  = 200.f;
        _chatView = [[NTESLiveChatView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        _chatView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _chatView.hidden = YES;
        _chatView.delegate = self;
    }
    return _chatView;
}

- (NTESLiveActionView *)actionView{
    if (!_actionView) {
        _actionView = [[NTESLiveActionView alloc] initWithFrame:CGRectZero];
        [_actionView sizeToFit];
        _actionView.delegate = self;
        _actionView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
        _actionView.hidden = YES;
        if ([NTESLiveManager sharedLiveManager].type == NIMNetCallMediaTypeAudio) {
            [self.actionView setActionType:NTESLiveActionTypeCamera disable:YES];
        }
    }
    return _actionView;
}

- (NTESLiveLikeView *)likeView{
    if (!_likeView) {
        CGFloat width  = 50.f;
        CGFloat height = 300.f;
        _likeView = [[NTESLiveLikeView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        _likeView.hidden = YES;
    }
    return _likeView;
}

- (NTESLivePresentView *)presentView{
    if(!_presentView){
        CGFloat width  = 200.f;
        CGFloat height = 96.f;
        _presentView = [[NTESLivePresentView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        _presentView.bottom = self.actionView.top;
        _presentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _presentView.hidden = YES;
    }
    return _presentView;
}


- (NTESLiveCoverView *)coverView{
    if (!_coverView) {
        _coverView = [[NTESLiveCoverView alloc] initWithFrame:self.bounds];
        _coverView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _coverView.hidden = YES;
        _coverView.delegate = self;
    }
    return _coverView;
}

- (NTESLiveBypassView *)bypassView{
    if (!_bypassView) {
        _bypassView = [[NTESLiveBypassView alloc] initWithFrame:CGRectZero];
        _bypassView.delegate = self;
        [_bypassView sizeToFit];
    }
    return _bypassView;
}

- (NTESAnchorMicView *)micView{
    if (!_micView) {
        _micView = [[NTESAnchorMicView alloc] initWithFrame:self.bounds];
        _micView.hidden = YES;
    }
    return _micView;
}

- (UILabel *)bypassNickLabel{
    if (!_bypassNickLabel) {
        _bypassNickLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _bypassNickLabel.backgroundColor = SHColorA(0, 0, 0, 0.3);
        _bypassNickLabel.textColor = [UIColor whiteColor];
        _bypassNickLabel.font = [UIFont systemFontOfSize:16.f];
    }
    return _bypassNickLabel;
}

- (UILabel *)roomIdLabel{
    if (!_roomIdLabel) {
        _roomIdLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _roomIdLabel.backgroundColor = SHColorA(0, 0, 0, 0.6);
        _roomIdLabel.textColor = [UIColor whiteColor];
        _roomIdLabel.font = [UIFont systemFontOfSize:9.f];
        _roomIdLabel.text =[NSString stringWithFormat:@"房间ID:%@",_roomId];
        _roomIdLabel.textAlignment = NSTextAlignmentCenter;
        _roomIdLabel.layer.masksToBounds = YES;
        _roomIdLabel.layer.cornerRadius = 8.f;
        CGRect rectTitle = [_roomIdLabel.text boundingRectWithSize:CGSizeMake(999, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:9.f]} context:nil];
        _roomIdLabel.height = rectTitle.size.height + 8.f ;
    }
    return _roomIdLabel;
}

- (NTESGLView *)glView{
    if (!_glView) {
        _glView = [[NTESGLView alloc] initWithFrame:self.bounds];
        _glView.contentMode = UIViewContentModeScaleAspectFill;
        _glView.hidden = YES;
    }
    return _glView;
}

- (NTESCameraZoomView*)cameraZoomView{
    if(!_cameraZoomView){
        _cameraZoomView = [[NTESCameraZoomView alloc]initWithFrame:CGRectZero];
        _cameraZoomView.hidden = YES;
    }
    return _cameraZoomView;
}

- (instancetype)initWithChatroom:(NSString *)chatroomId frame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _roomId = chatroomId;
        [self setupView];
    }
    return self;
}

- (void)setupView{
    _isBig = YES;
    [self addSubview:self.micView];
    [self addSubview:self.bypassNickLabel];
    [self addSubview:self.likeView];
    [self addSubview:self.presentView];
    [self addSubview:self.chatView];
    [self addSubview:self.glView];
    [self addSubview:self.actionView];
    [self addSubview:self.infoView];
    [self addSubview:self.textInputView];
    [self addSubview:self.coverView];
    [self addSubview:self.closeButton];
    [self addSubview:self.roomIdLabel];
    [self addSubview:self.cameraZoomView];
    [self addSubview:self.bypassView];
    [self adjustViewPosition];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(drageViewFrameChange:) name:@"DragViewFrameChangeNotification" object:nil];
//    [self.chatView.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
//    [self.micView addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionNew context:nil];
    _timer = [[NTESTimerHolder alloc] init];
    [_timer startTimer:60 delegate:self repeats:YES];
}

- (void)drageViewFrameChange:(NSNotification *)notifion{
    _isBig = [notifion.object integerValue];
    if (_isBig) {
        _infoView.hidden = NO;
        _roomIdLabel.hidden = NO;
        _actionView.hidden = NO;
        
        _chatView.hidden = NO;
        _likeView.hidden = NO;
        _presentView.hidden = NO;
        _closeButton.hidden = NO;
        _bypassView.hidden = NO;
        _actionView.hidden = NO;
        _bypassNickLabel.hidden = NO;
    }else{
        _infoView.hidden = YES;
        _roomIdLabel.hidden = YES;
        _actionView.hidden = YES;
        
        _chatView.hidden = YES;
        _likeView.hidden = YES;
        _presentView.hidden = YES;
        _closeButton.hidden = YES;
        _bypassView.hidden = YES;
        _actionView.hidden = YES;
        _bypassNickLabel.hidden = YES;
    }
    
//    _infoView.hidden = YES;
//    _roomIdLabel.hidden = YES;
//    _actionView.hidden = YES;
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    if (_keyBoradHeight){
        self.textInputView.bottom = self.height - _keyBoradHeight;
        self.chatView.bottom = self.textInputView.top;
    }else{
        CGFloat rowHeight = 35.f;
        self.textInputView.bottom = self.height + 44.f;
        if (_isActionViewMoveUp) {
            _chatView.bottom = self.height - 3 * rowHeight -  30.f;
        }else{
            _chatView.bottom = self.height - rowHeight - 20.f;
        }
    }
    CGFloat padding = 20.f;
    CGFloat delta = self.chatView.tableView.contentOffset.y;
    CGFloat bottom  = (delta < 0) ? self.chatView.top - delta : self.chatView.top;
    self.presentView.bottom = bottom - padding;
    
    self.roomIdLabel.top = self.infoView.bottom + 10.f;
    self.roomIdLabel.left = 10.f;
    self.roomIdLabel.width = [self getRoomIdLabelWidth];
    
    bottom = 100.f;
    self.bypassView.right  = self.width  - padding;
    self.bypassView.bottom = self.height - bottom;
    
    self.likeView.bottom = self.actionView.top;
    self.likeView.right  = self.width - 10.f;
    
    self.glView.size = self.size;
    
    self.bypassNickLabel.centerY  = self.infoView.centerY;
    self.bypassNickLabel.right = self.closeButton.left - padding;
    
    self.actionView.left = 0;
    self.actionView.bottom = self.height - 10.f;
    self.actionView.width = self.width;
    
    if ([NTESLiveManager sharedLiveManager].orientation == NIMVideoOrientationLandscapeRight) {
        self.cameraZoomView.centerY = self.infoView.centerY;
        self.cameraZoomView.centerX = self.width * .5f;
        self.cameraZoomView.height = 22.f;
        self.cameraZoomView.width = 225.f;
    }else{
        self.cameraZoomView.top = self.roomIdLabel.bottom + 20.f;
        self.cameraZoomView.centerX = self.width * .5f;
        self.cameraZoomView.height = 22.f;
        self.cameraZoomView.width = self.width - 2 * 30.f;
    }
}

- (CGFloat)getRoomIdLabelWidth{
    CGRect rectTitle = [_roomIdLabel.text boundingRectWithSize:CGSizeMake(999, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:9.f]} context:nil];
    CGFloat width = self.infoView.width;
    if (rectTitle.size.width > self.infoView.width) {
        width = rectTitle.size.width + 10.f;
    }
    return width;
}


#pragma mark - Notification
- (void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *info = notification.userInfo;
    _keyBoradHeight = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    [self layoutSubviews];
}

- (void)keyboardWillHide:(NSNotification *)notification{
    _keyBoradHeight = 0;
    [self layoutSubviews];
}

- (void)switchToWaitingUI{
    if ([NTESLiveManager sharedLiveManager].role == NTESLiveRoleAudience){
        [self switchToLinkingUI];
    }else{
        self.roomIdLabel.hidden = YES;
    }
    [self.bypassView refresh:nil status:NTESLiveBypassViewStatusNone];
    [self updateUserOnMic];
}

- (void)switchToLinkingUI{
    SHLog(@"switch to Linking UI");
    self.closeButton.hidden = NO;
    self.roomIdLabel.hidden = YES;
    
    [self.coverView refreshWithChatroom:self.roomId status:NTESLiveCoverStatusLinking];
    self.coverView.hidden = NO;
    [self.closeButton setImage:[UIImage imageNamed:@"icon_close_n"] forState:UIControlStateNormal];
    [self.closeButton setImage:[UIImage imageNamed:@"icon_close_p"] forState:UIControlStateHighlighted];
}

- (void)switchToPlayingUI{
    if (_isBig) {
        self.coverView.hidden = YES;
        self.textInputView.hidden = YES;
        self.micView.hidden = [NTESLiveManager sharedLiveManager].type != NTESLiveTypeAudio;
        
        self.infoView.hidden = NO;
        self.likeView.hidden = NO;
        self.chatView.hidden = NO;
        self.presentView.hidden = NO;
        self.actionView.hidden  = NO;
        self.roomIdLabel.hidden = NO;
    }

    
    NIMChatroom *room = [[NTESLiveManager sharedLiveManager] roomInfo:self.roomId];
    if (!room) {
        SHLog(@"chat room has not entered, ignore show playing UI");
        return;
    }
    
    
    
//    self.netStatusView.hidden = [NTESLiveManager sharedLiveManager].role == NTESLiveRoleAudience;
    if ([NTESLiveManager sharedLiveManager].connectorOnMic.type == NIMNetCallMediaTypeAudio) {
        [self.bypassView refresh:nil status:NTESLiveBypassViewStatusPlayingAndBypassingAudio];
    }else{
        [self.bypassView refresh:nil status:NTESLiveBypassViewStatusPlaying];
    }
    
    self.glView.hidden = YES;
    if ([NTESLiveManager sharedLiveManager].role == NTESLiveRoleAudience || [NTESLiveManager sharedLiveManager].type == NIMNetCallMediaTypeAudio) {
        [self.actionView setActionType:NTESLiveActionTypeCamera disable:YES];
        [self.actionView setActionType:NTESLiveActionTypeBeautify disable:YES];
        [self.actionView setActionType:NTESLiveActionTypeQuality disable:YES];
    }
    self.actionView.userInteractionEnabled = YES;
    [self.actionView setActionType:NTESLiveActionTypeInteract disable:NO];
    [self updateUserOnMic];
    [_closeButton setImage:[UIImage imageNamed:@"icon_close_p"] forState:UIControlStateNormal];
    [_closeButton setImage:[UIImage imageNamed:@"icon_close_n"] forState:UIControlStateHighlighted];
}

- (void)switchToEndUI{
    [self.coverView refreshWithChatroom:self.roomId status:NTESLiveCoverStatusFinished];
    self.coverView.hidden = NO;
    self.roomIdLabel.hidden = YES;
    
//    self.netStatusView.hidden = YES;
    if ([NTESLiveManager sharedLiveManager].role == NTESLiveRoleAnchor) {
        self.closeButton.hidden = YES;
    }else{
        self.closeButton.hidden = NO;
        [self.closeButton setImage:[UIImage imageNamed:@"icon_close_n"] forState:UIControlStateNormal];
        [self.closeButton setImage:[UIImage imageNamed:@"icon_close_p"] forState:UIControlStateHighlighted];
    }
}

- (void)updateUserOnMic{
    NTESMicConnector *connector = [NTESLiveManager sharedLiveManager].connectorOnMic;
    NSString *nick = connector.nick;
    self.bypassNickLabel.text = nick.length? [NSString stringWithFormat:@"连麦者：%@",nick] : @"";
    [self.bypassNickLabel sizeToFit];
    [self setNeedsLayout];
}

- (void)refreshChatroom:(NIMChatroom *)chatroom{
    _roomId = chatroom.roomId;
    [self.infoView refreshWithChatroom:chatroom];
    
    NSString *placeHolder = [NSString stringWithFormat:@"当前直播间ID:%@",chatroom.roomId];
    NTESGrowingTextView *textView = self.textInputView.textView;
    textView.editable = YES;
    textView.placeholderAttributedText = [[NSAttributedString alloc] initWithString:placeHolder attributes:@{NSFontAttributeName:textView.font,NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
}


- (void)updateQualityButton:(BOOL)isHigh{
    [self.actionView updateQualityButton:isHigh];
}

- (void)onClose:(id)sender{
    if ([NTESLiveManager sharedLiveManager].role == NTESLiveRoleAnchor) {
        if ([self.delegate respondsToSelector:@selector(onCloseLiving)]) {
            [self.delegate onCloseLiving];
        }
    }else{
        if ([self.delegate respondsToSelector:@selector(onClosePlaying)]) {
            [self.delegate onClosePlaying];
        }
    }
}

#pragma mark NTESLiveActionViewDelegate
- (void)onActionType:(NTESLiveActionType)type sender:(id)sender{
    switch (type) {
        case NTESLiveActionTypeLike:
            [self.likeView fireLike];
            break;
            
        case NTESLiveActionTypeChat:
        {
            self.textInputView.hidden = NO;
            UITextView *textview = (UITextView*)[self getTextViewFromTextInputView];
            [textview becomeFirstResponder];
        }
            break;
            
        case NTESLiveActionTypeMoveUp:
        {
            [self actionViewMoveToggle];
        }
            break;
            
        case NTESLiveActionTypeZoom:
        {
            self.cameraZoomView.hidden = !self.cameraZoomView.hidden;
            UIButton * button = (UIButton *)sender;
            [button setImage:[UIImage imageNamed:self.cameraZoomView.hidden ? @"icon_camera_zoom_n" :@"icon_camera_zoom_on_n"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:self.cameraZoomView.hidden ? @"icon_camera_zoom_p" :@"icon_camera_zoom_on_p"] forState:UIControlStateHighlighted];
        }
            break;
        default:
            break;
    }
    
    
    if ([self.delegate respondsToSelector:@selector(onActionType:sender:)]) {
        [self.delegate onActionType:type sender:sender];
    }
}

- (void)onSelectVideoQuality:(NTESLiveQuality)quality sender:(id)sender {
    if ([self.delegate respondsToSelector:@selector(onSelectVideoQuality:sender:)]) {
        BOOL success = [self.delegate onSelectVideoQuality:quality sender:sender];
        if (!success) {
            [self.actionView setLiveQuality:[NTESLiveManager sharedLiveManager].liveQuality];
        }
    }
}

#pragma mark - NTESTextInputViewDelegate
- (void)didSendText:(NSString *)text{
    if ([self.delegate respondsToSelector:@selector(didSendText:)]) {
        [self.delegate didSendText:text];
    }
}

- (void)willChangeHeight:(CGFloat)height{
    [self adjustViewPosition];
}

#pragma mark - NTESLiveBypassViewDelegate
- (void)didConfirmExitBypass{
    if ([self.delegate respondsToSelector:@selector(onCloseBypassing)]) {
        [self.delegate onCloseBypassing];
    }
}

#pragma mark - NTESTimerHolderDelegate
- (void)onNTESTimerFired:(NTESTimerHolder *)holder{
    __weak typeof(self) wself = self;
    SHLog(@"start refresh chatroom info");
    [[NIMSDK sharedSDK].chatroomManager fetchChatroomInfo:self.roomId completion:^(NSError *error, NIMChatroom *chatroom) {
        if (!error) {
            SHLog(@"refresh chatroom info OK");
            [wself.infoView refreshWithChatroom:chatroom];
        }else{
            SHLog(@"refresh chatroom info error : %@",error);
        }
    }];
}

#pragma mark - NTESLiveCoverViewDelegate
- (void)didPressBackButton{
  
}

#pragma mark - NTESLiveChatViewDelegate
- (void)onTapChatView:(CGPoint)point{
    if ([self.delegate respondsToSelector:@selector(onTapChatView:)]) {
        [self.delegate onTapChatView:point];
    }
}

- (void)adjustViewPosition{
    [self setNeedsLayout];
}

-(void)actionViewMoveToggle{
    _isActionViewMoveUp = !_isActionViewMoveUp;
    CGFloat rowHeight = 35;
    [self.actionView firstLineViewMoveToggle:_isActionViewMoveUp];
    if (_isActionViewMoveUp) {
        [UIView animateWithDuration:0.5 animations:^{
            _chatView.bottom = self.height - 3 * rowHeight - 30;
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _chatView.bottom = self.height - rowHeight - 20;
        }];
    }
}

- (UIView *)getTextViewFromTextInputView{
    for (UIView *view in self.textInputView.subviews) {
        if ([view isKindOfClass:[NTESGrowingTextView class]]) {
            for (UIView * subview in view.subviews) {
                if ([subview isKindOfClass:[UITextView class]]) {
                    return subview;
                }
            }
        }
    }
    return nil;
}

- (CGFloat)getActionViewHeight{
    return self.actionView.height;
}

#pragma mark 聊天
- (void)addMessages:(NSArray<NIMMessage *> *)messages{
    [self.chatView addMessages:messages];
}

- (void)addPresentMessages:(NSArray<NIMMessage *> *)messages{
    for (NIMMessage *message in messages) {
        [self.presentView addPresentMessage:message];
    }
}

- (void)fireLike{
    [self.likeView fireLike];
}

- (void)updateConnectorCount:(NSInteger)count{
    [self.actionView updateInteractButton:count];
}

- (void)switchToBypassLoadingUI:(NTESMicConnector *)connector{
    SHLog(@"switch to bypass loading UI connector id %@",connector.uid);
    [self.bypassView refresh:connector status:NTESLiveBypassViewStatusLoading];
    [self setNeedsLayout];
}

- (void)updateRemoteView:(NSData *)yuvData width:(NSUInteger)width height:(NSUInteger)height{
    if ([NTESLiveManager sharedLiveManager].role == NTESLiveRoleAnchor){
        [self.bypassView updateRemoteView:yuvData width:width height:height];
        if (_lastRemoteViewSize.width != width || _lastRemoteViewSize.height != height) {
            _lastRemoteViewSize = CGSizeMake(width, height);
            [self setNeedsLayout];
        }
    }else{
        [self.glView render:yuvData width:width height:height];
    }
}


- (void)switchToBypassStreamingUI:(NTESMicConnector *)connector{
    SHLog(@"switch to bypass streaming UI connector id %@",connector.uid);
    
    self.infoView.hidden = NO;
    self.likeView.hidden = NO;
    self.chatView.hidden = NO;
    self.presentView.hidden = NO;
    self.actionView.hidden  = NO;
    self.textInputView.hidden = NO;
    self.roomIdLabel.hidden = NO;
    
    NTESLiveBypassViewStatus status = connector.type == NIMNetCallMediaTypeAudio? NTESLiveBypassViewStatusStreamingAudio: NTESLiveBypassViewStatusStreamingVideo;
    [self.bypassView refresh:connector status:status];
    self.glView.hidden = YES;
    [self updateUserOnMic];
    [_closeButton setImage:[UIImage imageNamed:@"icon_close_p"] forState:UIControlStateNormal];
    [_closeButton setImage:[UIImage imageNamed:@"icon_close_n"] forState:UIControlStateHighlighted];
}


- (void)switchToBypassingUI:(NTESMicConnector *)connector{
    self.infoView.hidden = NO;
    self.likeView.hidden = NO;
    self.chatView.hidden = NO;
    self.presentView.hidden = NO;
    self.actionView.hidden  = NO;
    self.textInputView.hidden = NO;
//    self.netStatusView.hidden = YES;
    self.roomIdLabel.hidden = NO;
    self.bypassView.hidden = NO;
    NTESLiveBypassViewStatus status = connector.type == NIMNetCallMediaTypeAudio? NTESLiveBypassViewStatusLocalAudio: NTESLiveBypassViewStatusLocalVideo;
    [self.bypassView refresh:connector status:status];
    self.glView.hidden = NO;
    [self.actionView setActionType:NTESLiveActionTypeCamera disable: [NTESLiveManager sharedLiveManager].bypassType == NIMNetCallMediaTypeAudio];
    [self.actionView setActionType:NTESLiveActionTypeBeautify disable:[NTESLiveManager sharedLiveManager].bypassType == NIMNetCallMediaTypeAudio];
    [self.actionView setActionType:NTESLiveActionTypeInteract disable:YES];
    [self updateUserOnMic];
    [_closeButton setImage:[UIImage imageNamed:@"icon_close_p"] forState:UIControlStateNormal];
    [_closeButton setImage:[UIImage imageNamed:@"icon_close_n"] forState:UIControlStateHighlighted];
}


- (void)dealloc{
//    [_chatView.tableView removeObserver:self forKeyPath:@"contentOffset"];
//    [_micView removeObserver:self forKeyPath:@"hidden"];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
